package com.example.security.handler;/**
 * @Auther: liyipeng
 * @Date: 2020/1/8 13:43
 * @Description:
 */

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.security.SecurityUtil.ThreadLocalContext;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * <p>类名: MyMetaObjectHandler</p>
 * <p>描述:TODO</p>
 * <p>创建人: liyp</p>
 * <p>创建时间: 2020/1/8 13:43</p>
 * <p>@version 2.0  </p>
 * <p>修改内容: ......</p>
 * <p>修改说明: ......</p>
 * <p>修改时间: ......</p>
 * <p>修改人: ......</p>
 * <p>每次修改，请增加上述修改信息说明</>
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler{

    private static final Logger LOGGER = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        LOGGER.info("start insert fill ....");
        //版本号3.0.6以及之前的版本
        this.setFieldValByName("insertTime", LocalDateTime.now(), metaObject);
        //获取执行者账户id
        String acccountId = ThreadLocalContext.userInfo().getAccountId();
        this.setFieldValByName("insertSaId",acccountId, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LOGGER.info("start update fill ....");
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        String acccountId = ThreadLocalContext.userInfo().getAccountId();
        this.setFieldValByName("updateSaId",acccountId, metaObject);
    }
}
